/**
 * @开发版权 
 * @项目名称 VFCloud-PaaS
 * @版本信息 v1.0
 * @开发人员 zhous
 * @开发日期 2023-02-21
 * @修订日期
 * @描述  ApplicationClosedEventListener
 */
package com.vf.cloud.paas.common.listener;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

import com.vf.cloud.paas.app.agent.common.server.signalling.pool.StreamerPool;
import com.vf.cloud.paas.common.cirrus.Cirrus;
import com.vf.cloud.paas.common.util.ProssUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class ApplicationClosedEventListener implements ApplicationListener<ContextClosedEvent>{
	
	@Value("${agent.enabled}")
	private boolean agentEnabled;
	
	@Value("${master.enabled}")
	private boolean masterEnabled;
	
	@Value("${mysql.port}")
	private int mysqlPort;
	
	@Value("${agent.install.path}")
	private String installPath;
	
	@Value("${agent.project.path}")
	private String projectPath;
	
	@Override
	public void onApplicationEvent(ContextClosedEvent event) {
		log.info("销毁信令服务器."+installPath);
		StreamerPool.closeAll();
		//MYSQLUtil.stop();
		ProssUtil.stop();
		
		if(agentEnabled) {
			
			if(Cirrus.Streamer_Server!=null) {
				Cirrus.Streamer_Server.destroy();
			}
			if(Cirrus.Player_Server!=null) {
				Cirrus.Player_Server.destroy();
			}

			if(Cirrus.Stun_Server!=null) {
				Cirrus.Stun_Server.destroy();
			}
			if(Cirrus.Turn_Server!=null) {
				Cirrus.Turn_Server.destroy();
			}
			
			if(Cirrus.renderClient!=null) {
				Cirrus.renderClient.destroy();
			}
			
		}
		
		if(masterEnabled) {
			if(Cirrus.agentManagerServer!=null) {
				Cirrus.agentManagerServer.destroy();
			}
		}
	}


}
